package com.gz.moreaseManager.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.modules.entity.ProductSize;
import com.gz.moreaseManager.modules.mapper.ProductSizeMapper;
import com.gz.moreaseManager.modules.service.IProductSizeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 产品尺码信息 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-27
 */
@Service
public class ProductSizeServiceImpl extends ServiceImpl<ProductSizeMapper, ProductSize> implements IProductSizeService {

    @Resource
    private ProductSizeMapper productSizeMapper;


    /**
     * 根据产品ID新增/修改尺码
     * @param sizeList
     * @param productId
     */
    @Transactional
    @Override
    public void updateProductSize(List<ProductSize> sizeList, Integer productId) {

        //productId 不存在新增, 存在更新
        if(UtilTool.isNotBlank( sizeList )){
            for( ProductSize productSize : sizeList ){
                productSize.setProductId( productId );
                if( UtilTool.isBlank( productSize.getId())){
                    productSizeMapper.insert( productSize );
                }else{
                    productSizeMapper.updateById(productSize);
                }
            }
        }

    }

    /**
     * 根据产品ID删除尺码信息
     * @param productId
     */
    @Override
    public void deleteProductSizeByProductId(Integer productId) {
        if( UtilTool.isNotBlank( productId )){
            Wrapper wrapper = new EntityWrapper<ProductSize>();
            wrapper.eq("product_id",productId);
            productSizeMapper.delete(wrapper);
        }

    }

    /**
     * 根据产品ID获取尺码的列表
     * @param productId
     * @return
     */
    @Override
    public List<ProductSize> findSizeListByProductId(Integer productId) {
        if( UtilTool.isBlank( productId ))
            return null;
        Wrapper wrapper = new EntityWrapper<ProductSize>();
        wrapper.eq("product_id",productId);
        List<ProductSize> list = productSizeMapper.selectList(wrapper);
        if(UtilTool.isNotBlank( list ))
            return list;

        return null;
    }

    /**
     * 根据ids批量删除尺码
     * @param ids
     */
    /*@Override
    public void deleteSizeProductByIds(List<Integer> ids) {
        if( UtilTool.isNotBlank( ids )){
            productSizeMapper.deleteBatchIds(ids);
        }
    }*/
}
